package com.tengyun.middleplatform.trade.oatrade.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tengyun.middleplatform.trade.oatrade.mapper.OAMerchantMapper;
import com.tengyun.middleplatform.trade.oatrade.model.OAMerchant;
import com.tengyun.middleplatform.trade.oatrade.service.IDataHandleService;
import com.tengyun.middleplatform.trade.oatrade.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.linuxprobe.luava.http.HttpRequestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: likailun
 * @Description: 数据清洗服务
 * @Date: create in 2022/2/23 5:04 下午
 */
@Service
@Slf4j
public class IDataHandleServiceImpl implements IDataHandleService {
    @Value(value = "${trade.domain}")
    private String tradeDomain;
    @Resource
    private OAMerchantMapper oaMerchantMapper;


    @Override
    public void pushMerchant2OA(LocalDate today) {
        if (today == null) {
            today = LocalDate.now().minusDays(1);
        }
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        final String date = today.format(dtf);
        final HttpRequestUtils httpRequestUtils = new HttpRequestUtils();
        final CloseableHttpResponse request = httpRequestUtils.getRequest(tradeDomain + "/merchant/common/getMerchantByCreateDate?date=" + date);
        final Map map = HttpRequestUtils.responseDataConversion(request, Map.class);
        if(map==null || MapUtils.getIntValue(map,"ret")==1){
            log.info("查询商户出错，日期：{}",date);
            return;
        }
        List<Map<String, String>> dataMap = (List<Map<String, String>>) map.get("data");
        if(CollectionUtils.isEmpty(dataMap)){
            log.info("未查询到新增商户，日期：{}",date);
            return;
        }
        final Set<String> collect=new HashSet<>();
        for (Map<String, String> map2 : dataMap) {
            collect.add(MapUtils.getString(map2,"merchant_code"));
        }
        final QueryWrapper<OAMerchant> oaMerchantQueryWrapper = new QueryWrapper<>();
        oaMerchantQueryWrapper.in("field0002",collect);
        final List<OAMerchant> oaMerchants = oaMerchantMapper.selectList(oaMerchantQueryWrapper);
        Set<String> existMerchant=new HashSet<>();
        if(!CollectionUtils.isEmpty(oaMerchants)){
            existMerchant = oaMerchants.stream().map(OAMerchant::getMerchantCode).collect(Collectors.toSet());
        }

        final List<OAMerchant> saveOAMerchants = new ArrayList<>();
        for (Map<String, String> map3 : dataMap) {
            if(!existMerchant.contains(MapUtils.getString(map,"merchant_code"))){
                saveOAMerchants.add(new OAMerchant(MapUtils.getString(map3,"merchant_code"),MapUtils.getString(map,"merchant_name")));
            }
        }

        if(!CollectionUtils.isEmpty(saveOAMerchants)){
            oaMerchantMapper.saveBatch(saveOAMerchants);
        }
    }

}
